//
//  WmcAcceptanceTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/7.
//

import UIKit

/// 验收盖章水印
class WmcAcceptanceTemplateView: WmcBaseWatermarkTemplateView {
    lazy var chapterImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    lazy var lineView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 1
        view.layer.masksToBounds = true
        return view
    }()

    override func config() {
        super.config()
        defaultWidth = 225.wmc_autoHor
        let angle = -30
        chapterImageView.transform = CGAffineTransform(rotationAngle: CGFloat(angle.yp.angleToRadian))
        addSubview(chapterImageView)

        titleLabel.font = 16.yp.uiFont(weight: .medium)
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        titleLabel.transform = chapterImageView.transform
        addSubview(titleLabel)

        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
        addSubview(stackView)

        addSubview(lineView)
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)

        let alpha = calculateAlpha(watermarkModel.alpha, min: 0, max: 0.6)
        chapterImageView.alpha = alpha
        titleLabel.alpha = alpha

        chapterImageView.kf.setImage(with: watermarkModel.icon.yp.url, placeholder: "wm_chapter_icon".wmc_image)

        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .title: // 标题
                titleLabel.text = option.content
                // 标题色和线色图片色统一
                titleLabel.textColor = option.uiTextColor(.wmc_mainTheme)
                chapterImageView.yp.set(tintColor: titleLabel.textColor)
                lineView.backgroundColor = titleLabel.textColor

            case .time: // 时间
                let item = createDateItem(option: option,
                                          styleMode: styleMode,
                                          staticDate: watermarkModel.staticData.staticDate,
                                          maxTitleCount: maxTitleCount,
                                          dateFormat: .yyyy_P_MM_P_dd_week)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .brand: // 品牌图
                configOptionBrand(option)

            default: break
            }
        }

        drawViews.forEach { view in
            setShadow(view)
        }
        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews
    }

    override func makeConstraints() {
        chapterImageView.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(16)
            make.size.equalTo(CGSize(width: 98.wmc_autoHor, height: 77.wmc_autoVer))
        }

        titleLabel.snp.makeConstraints { make in
            make.center.size.equalTo(chapterImageView)
        }

        super.makeConstraints()

        stackView.snp.makeConstraints { make in
            make.top.equalTo(chapterImageView.snp.bottom).offset(8)
            make.left.equalTo(10)
            make.right.equalTo(-8)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }

        lineView.snp.makeConstraints { make in
            make.top.bottom.equalTo(stackView)
            make.left.equalTo(8)
            make.width.equalTo(2.6)
        }
    }
}
